import pygame

class Blade:
    def __init__(self):
        self.trail = []
        self.max_trail_length = 15
        # 鼠标滑过模式：默认一直处于切割状态
        self.cutting = True
        self.last_pos = None

    def update_pos(self, pos):
        # 记录位置并始终添加到轨迹中（鼠标滑过模式）
        self.last_pos = pos
        self.trail.append(pos)
        # 限制轨迹长度，保持性能
        if len(self.trail) > self.max_trail_length:
            self.trail.pop(0)

    def start_cut(self):
        # 保持切割状态
        self.cutting = True

    def end_cut(self):
        # 即使调用结束切割，也保持切割状态（鼠标滑过模式）
        self.cutting = True

    def update(self):
        # 逐渐减少旧轨迹点，但保留一些用于检测
        if len(self.trail) > 0:
            # 稍微延迟移除旧点，确保有足够的检测窗口
            if len(self.trail) > self.max_trail_length:
                self.trail.pop(0)

    def check_cut(self, rect):
        # 简化的碰撞检测：鼠标滑过就切割
        for pos in self.trail:
            # 计算点与矩形中心的距离
            rect_center = rect.center
            distance = ((pos[0] - rect_center[0]) ** 2 + (pos[1] - rect_center[1]) ** 2) ** 0.5
            # 增大检测范围，使鼠标滑过更容易切割
            if distance < rect.width * 2.0:
                return True
        return False

    def draw(self, screen):
        # 绘制明显的切割轨迹
        if len(self.trail) > 1:
            pygame.draw.lines(screen, (255, 255, 255), False, self.trail, 4)